package com.szholly.pro.watergas_server.ps.android.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.common.TableRowMap;
import com.szholly.pro.watergas_server.ps.data.QcWaterThanRow;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

/**
 * 数据可用性测试
 * 
 * 
 * 2017/3/30 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class QcDataValid {

	//监测站盲样考核校验
	public static void monitorStationBlindSpotSaveData(List<TableRowMap> list) throws Exception {
		MonitorStationBlindSpotSaveData validTestManager = new MonitorStationBlindSpotSaveData();
		validTestManager.init(list);
		validTestManager.makesureBaseDataValueValid();
		validTestManager.makesureMonitorTypeValid();
		validTestManager.makesureParameterValid();
		validTestManager.makesureNotRepeat();
		validTestManager.recycler();
	}

	public static class MonitorStationBlindSpotSaveData {
		// 生成公司ID列表
		private List<String> mCompanyIdList = new ArrayList<String>();
		// 每条数据之间的共性
		private String mMonitorTypeCode;// 监测类型编号
		private String mParameterCode;// 监测参数编号
		private String mSampleCode;// 样品编号
		private String mType;
		private String mValue;// 值

		private MonitorStationBlindSpotSaveData() {
		}

		// 释放内存
		private void recycler() {
			mCompanyIdList.clear();
			mMonitorTypeCode = null;
			mParameterCode = null;
			mSampleCode = null;
			mType = null;
		}

		// 初始化
		private void init(List<TableRowMap> list) {
			Assert.notEmpty(list, "数据不能为空！");
			TableRowMap androidTableMap = list.get(0);
			mMonitorTypeCode = androidTableMap.optString(QcWaterThanRow.FIELD_Monitortype);
			mParameterCode = androidTableMap.optString(QcWaterThanRow.FIELD_Parmname);
			mSampleCode = androidTableMap.optString(QcWaterThanRow.FIELD_Sampleid);
			mType = androidTableMap.optString(QcWaterThanRow.FIELD_Thantype);
			mValue = androidTableMap.optString(QcWaterThanRow.FIELD_Rtd);
			for (TableRowMap bean : list) {
				mCompanyIdList.add(bean.optString(QcWaterThanRow.FIELD_Companyid));
			}
		}

		// 确保不重复
		private void makesureNotRepeat() throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("sampleId", mSampleCode));
			parms.add(new GlobalParameter("type", mType));
			parms.add(new GlobalParameter("monitorType", mMonitorTypeCode));
			parms.add(new GlobalParameter("parameterId", mParameterCode));
			String sql = "select t1.COMPANYNAME,t1.COMPANYCODE from QC_WATER_THAN t\n"
					+ "left join QC_COMPANYINFO t1 on t.COMPANYID = t1.companycode\n"
					+ "where t.SAMPLEID = @sampleId and t.THANTYPE = @type and t.MONITORTYPE = @monitorType and t.PARMNAME = @parameterId";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows == null
					|| rows.size() == 0) {
				return;
			}
			// 按公司分类
			List<String> unValidCompanyNameList = new ArrayList<>();
			for (RowBase bean : rows) {
				String companyId = bean.getSafeString("COMPANYCODE");
				if (mCompanyIdList.contains(companyId)) {
					unValidCompanyNameList.add(bean.getSafeString("COMPANYNAME"));
				}
			}
			// 生成错误信息
			if (unValidCompanyNameList.size() > 0) {
				String errorMessage = "出现重复的数据，重复的公司有：\n";
				for (String companyName : unValidCompanyNameList) {
					errorMessage += companyName
							+ ",";
				}
				throw new IllegalArgumentException(errorMessage.substring(0, errorMessage.length()
						- 1));
			}
		}

		// 确保参数可用
		private void makesureParameterValid() throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("parameterCode", mParameterCode));
			parms.add(new GlobalParameter("monitorTypeCode", mMonitorTypeCode));
			String sql = "select t.COMPANYNAME, t.COMPANYCODE,t1.MONITORPARAMNAME from QC_COMPANYINFO t\n"
					+ "  left join (\n"
					+ "  select t1.COMPANYCODE,t.MONITORPARAMNAME from qc_monitorparam t\n"
					+ "  left join QC_COMPANYPARAMINFO t1 on t1.PARAMGUID = t.guid\n"
					+ "  where t.monitorparamcode = @parameterCode and t.monitortypecode = @monitorTypeCode\n"
					+ "  ) t1 on t.COMPANYCODE = t1.COMPANYCODE\n"
					+ "  group by t.COMPANYNAME, t.COMPANYCODE,t1.MONITORPARAMNAME";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows == null
					|| rows.size() == 0) {
				throw new SQLException("检测参数是否合法时,发生异常！");
			}
			// 按公司分类
			List<String> unValidCompanyNameList = new ArrayList<>();
			for (RowBase bean : rows) {
				String companyId = bean.getSafeString("COMPANYCODE");
				if (mCompanyIdList.contains(companyId)) {
					String parameterName = bean.getSafeString("MONITORPARAMNAME");
					if (StringUtils.IsNullOrSpace(parameterName)) {
						unValidCompanyNameList.add(bean.getSafeString("COMPANYNAME"));
					}
				}
			}
			// 生成错误信息
			if (unValidCompanyNameList.size() > 0) {
				String errorMessage = "以下公司不属于该参数类型\n";
				for (String companyName : unValidCompanyNameList) {
					errorMessage += companyName
							+ ",";
				}
				throw new IllegalArgumentException(errorMessage.substring(0, errorMessage.length()
						- 1));
			}
		}

		// 确保监测类型可用
		private void makesureMonitorTypeValid() throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("monitorTypeCode", mMonitorTypeCode));
			//搜索所有公司是否包含该监测类型，通过搜索出的监测类型是否为空来判断是否包含
			String sql = "select t.COMPANYNAME, t.COMPANYCODE,max(t1.pointtype) STATION_CATEGORY from QC_COMPANYINFO t\n"
					+ "left join qc_pointinfo t1 on t1.COMPANYCODE = t.COMPANYCODE and t1.pointtype = @monitorTypeCode\n"
					+ "group by t.COMPANYNAME, t.COMPANYCODE";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows == null
					|| rows.size() == 0) {
				throw new SQLException("检测监测类型是否合法时,发生异常！");
			}
			// 按公司分类
			List<String> unValidCompanyNameList = new ArrayList<>();
			for (RowBase bean : rows) {
				String companyId = bean.getSafeString("COMPANYCODE");
				if (mCompanyIdList.contains(companyId)) {
					String monitorTypeCode = bean.getSafeString("STATION_CATEGORY");
					if (StringUtils.IsNullOrSpace(monitorTypeCode)) {
						unValidCompanyNameList.add(bean.getSafeString("COMPANYNAME"));
					}
				}
			}
			// 生成错误信息
			if (unValidCompanyNameList.size() > 0) {
				String errorMessage = "以下公司不属于该监测类型\n";
				for (String companyName : unValidCompanyNameList) {
					errorMessage += companyName
							+ ",";
				}
				throw new IllegalArgumentException(errorMessage.substring(0, errorMessage.length()
						- 1));
			}
		}

		// 确保基础数据不能为空
		private void makesureBaseDataValueValid() throws Exception {
			ValidUtils.hasText(mMonitorTypeCode, "监测类型不能为空！");
			ValidUtils.hasText(mParameterCode, "参数不能为空！");
			ValidUtils.hasText(mSampleCode, "样品编号不能为空！");
			ValidUtils.hasText(mType, "数据类型不能为空！");
			ValidUtils.hasText(mValue, "值不能为空！");			
		}
	}
}
